package com.mbyte.easy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.admin.entity.*;
import com.mbyte.easy.admin.mapper.*;
import com.mbyte.easy.admin.service.IPptService;
import com.mbyte.easy.admin.service.IRelationService;
import com.mbyte.easy.common.constant.StatusPPTAudio;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.util.Utility;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ppt表 服务实现类
 * </p>
 *
 * @author lp
 * @since 2019-12-12
 */
@Slf4j
@Service
public class PptServiceImpl extends ServiceImpl<PptMapper, Ppt> implements IPptService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private AllotMapper allotMapper;
    @Autowired
    private CheckMapper checkMapper;
    @Autowired
    private DraftMapper draftMapper;
    @Autowired
    private PptMapper pptMapper;
    @Autowired
    private IRelationService relationService;
    @Autowired
    private RelationMapper relationMapper;


    @Override
    public IPage<Ppt> listPage(IPage<Ppt> page,QueryWrapper<Ppt> queryWrapper) {
        return page.setRecords(this.baseMapper.listPage(page,queryWrapper));
    }
    @Override
    public SysUser getUserByUserName(String username) {
        return sysUserMapper.selectByUsername(username);
    }
    /**
     * @Author 张雪
     * @Description 查询用户信息
     * @Date 2019/12/10 9:54
     **/
    @Override
    public List<SysUser> allotBefore(String username) {
        return sysUserMapper.allotBefore(username);
    }
    /**
     * @Author 张雪
     * @Description 查询当前登录用户所有下属用户
     * @Date 2019/12/10 9:55
     **/
    @Override
    public List<SysUser> selectAllWorker(String username){
        return sysUserMapper.selectAllWorker(username);

    }
/**
 *@author 孙祺至
 *@date 2019/12/16 10:50
*/
    @Override
    public List<Group> getGroupByUserId(Long userId) {
        return groupMapper.getGroupByUserId(userId);
    }

    @Override
    public List<Long> getAllotDraftIdByUserId(Long userId) {
        return allotMapper.getDraftIdByUserId(userId);
    }

    @Override
    public Group getGroupIdById(Long id) {
        return groupMapper.getGroupIdById(id);
    }

    @Override
    public int insertCheck(Check check) {
        return checkMapper.insert(check);
    }

    @Override
    public List<Check> getCheckById(Long id) {
        return checkMapper.getCheckById(id);
    }

    @Override

    public Long selectIdByUsername(String currentUsername) {
        return sysUserMapper.selectUserIdByUsername(currentUsername);
    }
    public List<Check> getChecksById(Long id) {
        return checkMapper.selectByUserId(id);
    }

    @Override
    public List<Check> getChecksByIdUnPass(Long id) {
        return checkMapper.getChecksByIdUnPass(id);
    }

    @Override
    public Draft GetGroupIdById(Long id) {
        return draftMapper.selectById(id);
    }
/**
 *@author 孙祺至
 *@date 2019/12/20 15:42
 */
    @Override
    public IPage leaderCheckIndex(Ppt ppt, Integer pageNo, Integer pageSize) {
        log.info("抽查 draft --> {}",ppt.toString());
//        List<SysUser> checkerList = relationService.allotBefore();//获取自己的组员
        QueryWrapper<Ppt> qw = new QueryWrapper<>();
//        qw.in("group_id", checkerList.stream().map(SysUser::getId));//获取所有自己分配的稿件
        //按自己的组ID查看、审核
        qw.like("group_id",ppt.getGroupId());
//        qw.like("ppt_maker",ppt.getPptMaker());
        //PPT是视频校对人通过的
        qw.eq("status_code", StatusPPTAudio.STATUS_PPT_VIDEOREADER_SUCCESS);
        IPage<Ppt> pptIPage = pptMapper.selectPage(new Page<>(pageNo,pageSize),qw);
        log.info("抽查 draftList --> {}",pptIPage.getRecords());
        return pptIPage;
    }
/**
 *@author 孙祺至
 *@date 2019/12/21 20:05
 */
    @Override
    public Map setGroupStatus() {
        Map<Integer,String> status = new HashMap<>();
        //获取自己的组员
//        List<SysUser> checkerList = relationService.PPTAllotBefore();
//        for (int i = 0; i < checkerList.size(); i++) {
//            status.put(checkerList.get(i).getId(),checkerList.get(i).getUsername());
//        }
        //按组ID分
        List<Relation>RelationList=relationMapper.selectByAllotId(sysUserMapper.selectByUsername(Utility.getCurrentUsername()).getId());
        log.info("========下属ID==================>>>>{}",RelationList);
        List<Group> groupByUserId=new ArrayList<>();
        for (int i = 0; i < RelationList.size(); i++) {
            List<Group> groupByUserId1 = groupMapper.getGroupByUserId(RelationList.get(i).getUserId());
            if (groupByUserId1.size()!=0){
                for (int i1 = 0; i1 < groupByUserId1.size(); i1++) {
                    groupByUserId.add(groupByUserId1.get(i1));
                }
            }

        }
        log.info("======下属为3 和5 的====所有group==========>>>>>>{}",groupByUserId);
        for (int i = 0; i < groupByUserId.size(); i++) {
            //1015
            List<Ppt> pptList=pptMapper.selectByGroupId(groupByUserId.get(i).getGroupId());
            log.info("根据groupID找到的所有的PPT============>>>>>{}",pptList);
            int mm=0;
            for (int i1 = 0; i1 < pptList.size(); i1++) {
                if (pptList.get(i1).getStatusCode()<StatusPPTAudio.STATUS_PPT_AUVELEADER_SUCCESS){
                    mm=1;
                }
                log.info("========这个groupId========",groupByUserId.get(i).getGroupId());
            }
            if (mm==0){
                groupByUserId.remove(i);
            }
        }
        for (Integer i = 0; i < groupByUserId.size(); i++) {
            status.put(i,groupByUserId.get(i).getGroupId());
        }
        log.info("=========statusMap==========>>>>>{}",status);
        return status;
    }
/**
 *@author 孙祺至
 *@date 2019/12/20 15:42
 */
    @Override
    public Map setDraftCheckStatus() {
        Map<Integer,String> status = new HashMap<Integer,String>();
        status.put(StatusPPTAudio.STATUS_PPT_AUVELEADER_SUCCESS,"已通过");
        status.put(StatusPPTAudio.STATUS_PPT_AUVELEADER_FAILED,"未通过");
        status.put(StatusPPTAudio.STATUS_PPT_VIDEOREADER_SUCCESS,"未审核");
        return status;
    }
/**
 *@author 孙祺至
 *@date 2019/12/23 9:47
 */
    @Override
    public List<Long> getChildrenIdByParentId(Long id) {
        return relationMapper.getChildrenIdByParentId(id);
    }

    @Override
    public Ppt selectGroupIdById(Long id) {
        return pptMapper.selectById(id);
    }

    @Override
    public List<Check> getChecksByIdPass(Long id) {
        return checkMapper.getChecksByIdPass(id);
    }

    @Override
    public Long selectDIdByID(Long arrayId) {
        return null;
    }

    @Override
    public Ppt getBydraftId(Long id) {
        return pptMapper.selectByDraftId(id);
    }

    @Override
    public boolean updateStatusByDraftId(Ppt ppt) {
        return pptMapper.updateStatusByDraftId(ppt);
    }


}
